package com.sunxu.spring.scope;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 孙许
 * @date 2018-07-14
 * @description 测试线程安全
 */
public class Application2 {

    public static final int THREAD_NUM = 300;

    public static void main(String[] args) throws InterruptedException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        ExecutorService service = Executors.newFixedThreadPool(20);
        CountDownLatch countDownLatch = new CountDownLatch(THREAD_NUM);

        for (int i = 0; i < THREAD_NUM; i++) {
            service.execute(() -> {
                MessagePrinter messagePrinter = context.getBean(MessagePrinter.class);
                System.out.println(messagePrinter.getService());
            });
            countDownLatch.countDown();
        }
        countDownLatch.await();
        service.shutdown();
        System.out.println("执行完毕");
    }
}
